package com.mukefile.action;

import com.mukefile.pojo.MukeFile;
import com.mukefile.pojo.User;
import com.mukefile.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: kenewstar
 * @Description: 用户Action类
 * @Date:Created in 2020/5/4
 */
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {

    @Autowired
    private UserService userService;
    /**
     * 获取Request对象
     */
    private final Map<String, Object> request = (Map<String, Object>)ActionContext.getContext().get("request");
    /**
     * 获取Session对象
     */
    private final Map<String,Object> session = ActionContext.getContext().getSession();

    /**
     * 用于接收前台的实体对象数据
     */
    private User user = new User();

    @Override
    public User getModel() {
        return this.user;
    }

    /**
     * 用户登录
     * 判断前台的用户名与密码是否匹配
     * @return 跳转至main.ftl页面
     */
    public String userLogin(){
        //md5加密
        this.user.setPassword(new Md5Hash(this.user.getPassword()
                ,"kenewstar").toString());
        this.user = this.userService.userLogin(this.user);
        if (this.user==null){
            request.put("msg","用户名或密码错误！");
            return "login";
        }
        session.put("user",user);
        return "main";
    }

    /**
     * 用户退出
     * 用户调转至登录页面
     * @return 返回至login.ftl
     */
    public String userExit(){
        this.userService.userExit(session);
        return "login";
    }

    /**
     * 用户修改密码
     * @return 跳转至登录页面
     */
    public String updatePwd(){
        User user = (User)this.session.get("user");
        int i = this.userService.updatePwd(
                new Md5Hash(this.user.getPassword(),"kenewstar").toString(), user);
        if(i==1){
            return "login";
        }else {
            request.put("msg","您的密码修改失败");
            return "main";
        }
    }
    private String filename;
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }

    /**
     * 根据文件名关键字搜索文件
     * @return 返回文件列表
     */
    public String searchFile(){
        //传入文件名，用户对象
        User user = (User)this.session.get("user");
        List<MukeFile> list = new ArrayList<>();
        list = this.userService.searchFile(this.filename,user);
        if (list.isEmpty()){
            this.request.put("msg","查无此文件");
        }
        this.request.put("list",list);
        return "fileOperate";
    }
    /**
     * 密码校验
     * 校验输入原密码是否与用户密码一致
     */
    public String pwdValidate() throws IOException {
        HttpServletResponse resp = ServletActionContext.getResponse();
        String password = new Md5Hash(this.user.getPassword(),"kenewstar").toString();
        User user = (User)this.session.get("user");
        if (user.getPassword().equals(password)){
            //密码相等
            resp.getWriter().write("");
        }else {
            resp.getWriter().write("ERROR");
        }
        return null;
    }

    /**
     * 跳转至主页面
     * @return 主页面
     */
    public String main(){
        return "main";
    }
    /**
     * 跳转至上传文件页面
     * @return 文件上传页面
     */
    public String upload(){
        return "upload";
    }
}
